import { createStore, bindActionCreators, applyMiddleware } from "../redux";
import { counterDecrAction, counterIncrAction } from "./action/counterAction";
import * as counterActions from "./action/counterAction";
import { reducer } from "./reducer";

// 中间件
const logger1 = store => next => action => {
    //! store 非完整仓库对象只包含 getState, dipatch 两个属性
    //! next 实际就是 dispatch 派发函数
    //! action 实际就是派发的动作类型
    // console.log(store, next, action);
    console.log("旧数据1==>", store.getState());
    next(action);
    console.log("新数据1==>", store.getState());
}
const logger2 = store => next => action => {
    console.log("旧数据2==>", store.getState());
    next(action);
    console.log("新数据2==>", store.getState());
}

// 创建方式1
// const store = createStore(reducer, applyMiddleware(logger1, logger2));
// 创建方式2 (applyMiddleware方法实现是根据下面函数形式实现，上面只是一种变换最终还是会根据下面形式完成)
const store = applyMiddleware(logger1, logger2)(createStore)(reducer);

// 添加监听器
store.subscribe(() => {
    console.log("仓库状态==>", store.getState())
});

// 派发动作
store.dispatch(counterIncrAction());
store.dispatch(counterIncrAction());
store.dispatch(counterDecrAction());
store.dispatch(counterDecrAction());

// 增强派发功能参数一为函数
const counterIncr = bindActionCreators(counterIncrAction, store.dispatch);
counterIncr();

// 增强派发功能参数一为对象
const counter = bindActionCreators(counterActions, store.dispatch);
counter.counterDecrAction();


